package com.timi_licai.timi_licai_account.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.timi_licai.timi_licai_account.AddActivity;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.adapter.TimeLineBaseAdapter;
import com.timi_licai.timi_licai_account.listener.MyCallback;
import com.timi_licai.timi_licai_account.model.ConsumeDetails;
import com.timi_licai.timi_licai_account.utils.DatabaseQueryUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * MainFragment
 */
public class MainFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {

    private PullToRefreshListView refreshListView;
    private ListView listView;
    private ImageView imgAdd;
    private ImageButton btnAdd;
    private MyCallback content;
    private TimeLineBaseAdapter adapter;
    private List<ConsumeDetails> listViewData;
    private Animation animation;
    private TextView tvbookname;
    private TextView mBookInCome;
    private TextView mBookOut;
    private String bookname;

    public MainFragment() {
        // Required empty public constructor
    }

    public MainFragment(MyCallback content) {
        this.content = content;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        imgAdd = (ImageView) view.findViewById(R.id.image_add);
        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.mainfragment_translate);
        tvbookname = (TextView) view.findViewById(R.id.tv_bookName);
        btnAdd = (ImageButton) view.findViewById(R.id.btn_add);
        mBookInCome = (TextView) view.findViewById(R.id.tv_book1_income);
        mBookOut = (TextView) view.findViewById(R.id.tv_book1_out);
        imgAdd.setOnClickListener(this);
        btnAdd.setOnClickListener(this);

        refreshListView = (PullToRefreshListView) view.findViewById(R.id.refreshListView);
        //得到下拉刷新的refreshView中的listView
        listView = refreshListView.getRefreshableView();
        refreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        // 设置加载图标
        refreshListView.setLoadingDrawable(null);
        refreshListView.setVerticalScrollBarEnabled(true);
        refreshListView.setOnRefreshListener(this);
        listViewData = new ArrayList<ConsumeDetails>();

        adapter = new TimeLineBaseAdapter(getActivity(), listViewData);
        listView.setAdapter(adapter);

        listView.setVerticalScrollBarEnabled(true);//不管有没有活动，滚动条都隐藏
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setDivider(null);
        listView.setOnItemClickListener(this);

        View menu = view.findViewById(R.id.menu_btn);
        menu.setOnClickListener(this);

        return view;
    }

    //存储 position 用于typeImg判断是否点击的是同一个
    private int location = -1;

    @Override
    public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
        View typeImg = view.findViewById(R.id.time_line_typeimg);
    }

    @Override
    public void onClick(View v) {
        final Intent intent;
        switch (v.getId()) {
            case R.id.menu_btn:
                content.set("startslidingmenu");
                break;
            default:
                intent = new Intent(getActivity(), AddActivity.class);
                btnAdd.startAnimation(animation);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.activity_in_anim, R.anim.activity_exit_anim);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                break;

        }

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        Intent intent = new Intent(getActivity(), AddActivity.class);
        startActivity(intent);

        refreshListView.onRefreshComplete();
        getActivity().overridePendingTransition(R.anim.activity_exit_anim, R.anim.activity_in_anim);

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

    }

    /**
     * 向Adapter中插入数据
     *
     * @param id
     */
    public void updateBookId(int id) {
        listViewData.clear();
        tvbookname.setText("");
        mBookInCome.setText(0.00 + "");
        mBookOut.setText(0.00 + "");
        int book_id = 0;
        //得到选中账本的id
        book_id = DatabaseQueryUtils.getSelectedAccountBook();
        if (book_id != 0) {
            //得到选中账本的名字
            String bookName = DatabaseQueryUtils.getSelectedAccountBookName(book_id);
            tvbookname.setText(bookName);
        }
        //设置支出的总费用
        float outputTotal = DatabaseQueryUtils.getOutputTotal(book_id);
        mBookOut.setText(outputTotal + "");
        float inputTotal = DatabaseQueryUtils.getInputTotal(book_id);
        mBookInCome.setText(inputTotal + "");
        //根据Id获取账本详情
        List<ConsumeDetails> consumeDetails = DatabaseQueryUtils.getConsumeDetails(book_id);
        listViewData.addAll(consumeDetails);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onResume() {
        super.onResume();
        int selectedBookId = DatabaseQueryUtils.getSelectedAccountBook();
        updateBookId(selectedBookId);
    }
}
